package com.pinyoumei.app.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.pinyoumei.app.R;
import com.pinyoumei.app.bean.BaseBean;
import com.pinyoumei.app.network.Constants;
import com.pinyoumei.app.utils.EventBusUtils;
import com.pinyoumei.app.utils.SPUtils;
import com.pinyoumei.app.utils.SignatureUtils;
import com.pinyoumei.app.utils.ToastUtils;
import com.pinyoumei.app.utils.event.ReshEvent;
import com.pinyoumei.app.view.LoadingDailog;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.base.Request;

import java.util.HashMap;
import java.util.Map;

import rx.Subscriber;

/**
 * 修改昵称
 */
public class ChangeNicknNmeActivity extends BaseActivity implements View.OnClickListener{
    private EditText edittext_nickname;
    private ImageView image_delete;
    private RelativeLayout relative_save;
    private RelativeLayout relative_finish;
    private LoadingDailog loadingDailog;
    private Subscriber subscriber;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_layout_changenickname);
        initwidget();
        LoadingDailog.Builder builder = new LoadingDailog.Builder(this)
                .setMessage("加载中...")
                .setCancelable(true);
        loadingDailog = builder.create();
        edittext_nickname.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if(s.length()>0){
                    image_delete.setVisibility(View.VISIBLE);
                }else{
                    image_delete.setVisibility(View.GONE);
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
       switch (v.getId()){
           case R.id.image_delete:
               edittext_nickname.setText("");
               break;
           case R.id.relative_save:
               //昵称修改
               nick_name(edittext_nickname.getText().toString().trim());
               break;
           case R.id.relative_finish:
               finish();
               break;
       }
    }
    private void initwidget(){
        edittext_nickname = findViewById(R.id.edittext_nickname);
        image_delete = findViewById(R.id.image_delete);
        image_delete.setOnClickListener(this);
        image_delete.setVisibility(View.GONE);
        relative_save = findViewById(R.id.relative_save);
        relative_save.setOnClickListener(this);
        relative_finish = findViewById(R.id.relative_finish);
        relative_finish.setOnClickListener(this);
    }
    private void nick_name(String user_nick_name) {
        Map<String, Object> map = new HashMap<>();
        map.put("user_id", SPUtils.getAppUserId(ChangeNicknNmeActivity.this));
        map.put("user_nick_name",user_nick_name);

        OkGo.<String>post(Constants.SETTING_NICKNAME)
                .params("user_id", SPUtils.getAppUserId(ChangeNicknNmeActivity.this))
                .params("user_nick_name",user_nick_name)
                .params("sign", SignatureUtils.createSignature(map, Constants.SIGN_SECRET))
                .execute(new StringCallback() {


                    @Override
                    public void onStart(Request<String, ? extends Request> request) {
                        super.onStart(request);
                        loadingDailog.show();
                    }


                    @Override
                    public void onSuccess(Response<String> response) {
                        setData(response);

                    }

                    private void setData(Response<String> response) {
                        loadingDailog.dismiss();

                        BaseBean settingUserBean = gson.fromJson(response.body(), BaseBean.class);

                        if (settingUserBean.getCode() >= 0) {
                            EventBusUtils.post(new ReshEvent("1"));
                            ToastUtils.showShort(settingUserBean.getMsg());
                            finish();
                        } else {
                            ToastUtils.showShort(settingUserBean.getMsg());
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        loadingDailog.dismiss();
                    }
                });


    }

}
